package com.oocl.euc.service.impl;

import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oocl.euc.dao.MerchantDao;
import com.oocl.euc.dao.impl.MerchantDaoImpl;
import com.oocl.euc.entity.Merchant;
import com.oocl.euc.pagination.BaseConditionQuery;
import com.oocl.euc.pagination.PaginationService;
import com.oocl.euc.pagination.SearchFilter;
import com.oocl.euc.service.MerchantService;
@Service("merchantService")
@Transactional
public class MerhcantServiceImpl extends PaginationService<Merchant> implements MerchantService {
	@Autowired
	private MerchantDaoImpl md;

	@Override
	public Integer merchantRegist(Merchant m) {
		return md.save(m);
	}

	@Override
	public boolean merchantLogin(String account, String password) {
		List<Merchant> merchants=md.findAll();
		boolean mark=false;
		for(Merchant m:merchants){
			if(m.getAccount().equals(account)&&m.getPassword().equals(password)){
				mark=true; 
				break;
			}
		}
		return mark;
	}

	@Override
	public Integer deleteMerchant(String mid) {
		return md.delete(md.findById(mid));
	}

	@Override
	public Merchant findMesssrchantByMid(String mid) {
		return md.findById(mid);
	}

	@Override
	public List<Merchant> findAllMerchants() {
		return md.findAll();
	}

	@Override
	public boolean existAccount(String account) {
		Criteria criteria=Criteria.where("account").is(account);
		return md.exists(new Query(criteria));
	}

	@Override
	public Page<Merchant> findMerchantsWithPage(Integer limit, Integer page, String searchFilters, String sort) {
		BaseConditionQuery<Merchant> query=getQuery(limit, page, searchFilters, sort);
		return md.findAll(query);
	}

	@Override
	public Merchant findMerchantByAccount(String account) {
		return md.findOne(new Query(Criteria.where("account").is(account)));
	}
}
